define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prizesetting/index',
                    add_url: 'prizesetting/add',
                    edit_url: 'prizesetting/edit',
                    del_url: 'prizesetting/del',
                    multi_url: 'prizesetting/multi',
                    table: 'prize_setting',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'prize.name', title: __('Category')},
                        {field: 'type', title: __('Type'),formatter: Controller.api.formatter.type,},
                        {field: 'name', title: __('Name')},
                        {field: 'img', title: __('Img'),formatter: Table.api.formatter.image, },
                        {field: 'percent', title: __('Percent'),formatter: Controller.api.formatter.percent},
                        {field: 'order', title: __('Order')},
                        {field: 'addtime', title: __('Addtime'), formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), formatter: Controller.api.formatter.status},
                        {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                percent:function(value,row,index){
                    return value+'%';
                },
                type:function(value,row,index){
                    var res ='';
                    switch(value){
                        case '0' : res = '红包'; break;
                        case '1' : res = '实物'; break;
                        case '2' : res = '其他'; break;
                        case '3' : res = '加息卡'; break;
                    }
                    return res;
                },
                status:function(value,row,index){
                    if(value==1){
                        return "有效";
                    }else{
                        return "失效";
                    }
                }
            }
        }
    };
    return Controller;
});